﻿package little.bunny;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnKeyListener;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MyLogin extends Activity {
	private Button btn_login;
	private Button btn_register;
	private Button btn_cancel;
	private EditText txtInputUser;
	private EditText txtInputPass;

	private Button backButton;
	private CheckBox chk_pass;
	
	private final String serviceNameSpace = "http://littlebunny.com/";
	private final String method_name = "CustomLogin";
	private final String serviceUrl = "http://www.littlebunny.com.cn/bunny_service/bunny_service.asmx";
	private final String SOAP_ACTION = "http://littlebunny.com/CustomLogin";
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_login);
		
		txtInputUser = (EditText) findViewById(R.id.edit_login_account);
		txtInputPass = (EditText) findViewById(R.id.edit_login_pwd);

		chk_pass = (CheckBox) findViewById(R.id.chksave_pass);
		
		SharedPreferences sharedata = getSharedPreferences("user_data", 0);  

		String s_user = sharedata.getString("name", null);
		String s_pass = sharedata.getString("pass", null);
		
		//************测试数据*******
		//txtInputCard.setText("000120");
		//txtInputPass.setText("15673393");
		//*************************
				
		btn_login = (Button) findViewById(R.id.btn_login);
		
		btn_login.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				user_login();
			}
		});
		
		btn_register = (Button) findViewById(R.id.login_btn_register);
		
		btn_register.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				Intent in=new Intent(MyLogin.this,MyRegister.class);
				MyLogin.this.startActivity(in);
			}
		});
		
		btn_cancel = (Button) findViewById(R.id.login_btn_cancel);
		btn_cancel.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		backButton = (Button) findViewById(R.id.back_button);
		backButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		if (s_user != null && s_user.length() > 0)
		{
			txtInputUser.setText(s_user);
			
			if (s_pass !=null && s_pass.length()>0)
			{
				chk_pass.setChecked(true);
				txtInputPass.setText(s_pass);
				
				//自动登录
				//user_login();
			}
		}
	}

	private void user_login() 
	{
		//card_id:000120,pass:15673393

		if (txtInputUser.getText().toString().length() == 0) {
			ShowMsg("请输入账号.");
			return;
		}

		if (txtInputPass.getText().toString().length() == 0) {
			ShowMsg("请输入密码.");
			return;
		}
		
		if (NetFunction.CheckNetStatus(this)==false)
		{
			ShowMsg("没有找到有效的网络.");
			return;
		}
		
		//Toast.makeText(this, "读取用户信息...", Toast.LENGTH_LONG).show();
		
		final ProgressDialog pDialog = ProgressDialog.show(this, "请稍等",
				"检测用户信息...", true);
		
		pDialog.setOnKeyListener(new OnKeyListener() {
			
			public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				if (keyCode == KeyEvent.KEYCODE_BACK){
					pDialog.dismiss();
				}
				return false;
			}
		});
		Thread networkThread = new Thread() {
			@Override
			public void run() {
				SoapObject request = new SoapObject(serviceNameSpace, method_name);
				request.addProperty("user_id", txtInputUser.getText().toString());
				request.addProperty("u_pass", txtInputPass.getText().toString());

				SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
				envelope.dotNet = true;
				envelope.setOutputSoapObject(request);
				
				try {
					HttpTransportSE ht = new HttpTransportSE(serviceUrl);
					ht.call(SOAP_ACTION, envelope);

					final SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
					final String result = response.toString();
					
					//Toast.makeText(this, "读取完毕.", Toast.LENGTH_LONG).show();
					
					pDialog.dismiss();// 隐藏进度条
					
					runOnUiThread(new Runnable() {
						public void run() {
							if (result.startsWith("99:", 0))
							{
								ShowMyDialog("用户登录",result.substring(3));
							}
							else
							{
								if (result.length()==0)
								{
									ShowMyDialog("用户登录","网络错误");
								}
															
								
								//保存密码,保存用户卡号
								SharedPreferences.Editor sharedata1 = getSharedPreferences("user_data", 0).edit();
								sharedata1.clear();
								sharedata1.putString("name",txtInputUser.getText().toString());
								sharedata1.putString("pass_key",txtInputPass.getText().toString());//系统后台使用的数据
								
								if (chk_pass.isChecked())
									sharedata1.putString("pass",txtInputPass.getText().toString());
								else
									sharedata1.putString("pass","");
								
								sharedata1.commit();
								
								if (result.substring(3).length()==0)//没有绑定卡，继续绑定
								{
									Intent myIntent = new Intent (MyLogin.this, MyBindCard.class);
									//把用户名传给下一个activity
									//Bundle bdle = new Bundle(); 
									//bdle.putString("KEY_USER",txtInputUser.getText().toString());
									//bdle.putString("KEY_CARD","");
									//myIntent.putExtras(bdle);
									
				                    startActivity(myIntent);
								}
								else
								{
									Intent myIntent=new Intent(MyLogin.this,MyHome.class);
									startActivity(myIntent);
								}
							}
						}
					});
				} catch (Exception e) {
					pDialog.dismiss();
					e.printStackTrace();
					ShowMyDialog("网络信号异常","请查看网络连接，稍候再试...");
					
					//Toast.makeText(this, "网络错误,请检查能否访问网络", Toast.LENGTH_LONG).show();
					//TextView txt1;
					//txt1 = (TextView) findViewById(R.id.textShowJiFen);
					//txt1.setText("网络错误,请检查能否访问网络");
				}
			}
		};

		networkThread.start();
	}
	
	private void ShowMsg(String msg)
	{
		Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
	}
	
	private void ShowMyDialog(String title, String info)
	{
		new AlertDialog.Builder(MyLogin.this)
		.setTitle(title)
		.setMessage(info)
		.setPositiveButton("确定",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,
							int which) {
						//MyLogin.this.finish();
					}
				}).show();
	}
}
